package whu.cn.whushare.ui.main.activity;

import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.androidplus.ui.ToastManager;
import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.List;

import whu.cn.whushare.R;
import whu.cn.whushare.net.DownloadParams;
import whu.cn.whushare.net.Downloader;
import whu.cn.whushare.ui.common.LoadingHelper;
import whu.cn.whushare.ui.common.MainReShowFragment;
import whu.cn.whushare.ui.main.activity.adapter.AllActivityListAdapter;
import whu.cn.whushare.ui.main.activity.bean.Activity;
import whu.cn.whushare.ui.main.activity.bean.AllActivity;
import whu.cn.whushare.ui.main.known.QuestionDetailActivity;
import whu.cn.whushare.ui.main.known.QuestionListActivity;
import whu.cn.whushare.ui.main.known.adapter.AllQuestionListAdapter;
import whu.cn.whushare.ui.main.known.bean.AllQuestion;
import whu.cn.whushare.util.Utils;

/**
 * Created by xiaoping on 2014/12/12.
 */
public class ActivitiesFragment extends MainReShowFragment implements View.OnClickListener {
    @ViewMapping(id = R.id.all_activity_list)
    private PullToRefreshListView mRefreshListView;
    private AllActivityListAdapter adapter;
    private LoadingHelper loadingHelper;
    private boolean mHasInited;
    private List<AllActivity> mActivities;
    private int mCurPage = 1;
    private int mTotal=20000;
    private final static int PAGE_SIZE = 5;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        initParams();
        View root = inflater.inflate(R.layout.activity_layout, container, false);
        initViews(root);
        return root;
    }

    private void initParams() {

    }

    private void initViews(View root) {
        ViewMappingUtil.mapView(this, root);
        getTopBarWithoutBack(root, R.id.top_bar);
        mTopBar.goBack.setVisibility(View.VISIBLE);
        mTopBar.goBack.setImageResource(R.drawable.search1);
        mTopBar.rightText.setVisibility(View.VISIBLE);
        mTopBar.rightText.setText(R.string.launch_activity);
        mTopBar.searchLayout.setVisibility(View.INVISIBLE);
        mRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        mRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> listViewPullToRefreshBase) {
                requestActivityList(1);
                Utils.resetRefreshLabel(getActivity(), mRefreshListView);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> listViewPullToRefreshBase) {
                if (mCurPage * PAGE_SIZE >=mTotal) {
                    Utils.setRefreshLabelToLast(getActivity(), mRefreshListView);
                    Utils.COMMON_HANDLER.sendMessageDelayed(
                            Message.obtain(Utils.COMMON_HANDLER, Utils.REFRESH_TO_COMPLETE, mRefreshListView), 500);
                } else {
                    requestActivityList(mCurPage + 1);



                }
            }
        });

    }
    private void requestActivityList(int page) {
        Downloader.getInstance(getActivity()).getAllActivityList(page, this, page);
        loadingHelper.showLoadingView(mHasInited);
    }
    @Override
    public <T> void onSuccess(DownloadParams.PARSING_TYPE type, T response, Object obj) {
        super.onSuccess(type, response, obj);
        mCurPage = (int) obj;
        switch (type) {
            case ALL_ACTIVITY_LIST:
                List<AllActivity> activities = (List<AllActivity>) response;

                if (adapter == null || mCurPage == 1) {
                    mActivities =activities;
                    adapter = new AllActivityListAdapter(getActivity(), mActivities);
                    mRefreshListView.setAdapter(adapter);
                } else {
                    mActivities.addAll(activities);
                    adapter.notifyDataSetChanged();
                }
                if(activities.size()<PAGE_SIZE)
                    mTotal=mActivities.size();
                mHasInited = true;
                loadingHelper.showContentView();
                mRefreshListView.onRefreshComplete();

                break;
            default:
                break;
        }
    }

    @Override
    public void onFail(DownloadParams.PARSING_TYPE type, VolleyError error) {
        super.onFail(type, error);
        if (!mHasInited) {
            loadingHelper.showRetryView();
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initActions();
        loadingHelper = new LoadingHelper(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               requestActivityList(mCurPage);
            }
        });
        loadingHelper.onCreateView(LayoutInflater.from(getActivity()), mRefreshListView);
        requestActivityList(mCurPage);
    }

    private void initActions() {
        mTopBar.searchTxt.setOnClickListener(this);
        mTopBar.goBack.setOnClickListener(this);
        mTopBar.rightText.setOnClickListener(this);
        mRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                AllActivity allActivity = (AllActivity) adapter.getItem(arg2 - 1);
                String activityId = String.valueOf(allActivity.getActivity().getActivityId());
                startActivityForResult(ActivityDetailActivity.getStartIntent(getActivity(), activityId), 1);
            }

        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_right_txt:
                startActivityForResult(ActivityLaunchActivity.getStartIntent(getActivity()),1);
                break;

            case R.id.go_back_btn:
                startActivity(ActivityListActivity.getStartIntent(getActivity(), "activity"));
                break;
            default:
                break;
        }
    }
}
